﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class PaymentHistory
    {

        public static void update(PaymentHistoryEntity paymentHistory)
        {
            DbHelper.ExecuteNonQuery("p_PaymentHistory_update",
              paymentHistory.PayId,              
              paymentHistory.Status,             
              paymentHistory.AdminId);
        }

        public static PaymentHistoryEntity getByPayId(int payId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_PaymentHistory_getByPayId", payId);
            PaymentHistoryEntity _paymentHistory = new PaymentHistoryEntity();
            if (dr != null)
            {
                _paymentHistory.PayId = payId;
                _paymentHistory.Amount = Convert.ToDecimal(dr["amount"]);
                _paymentHistory.CompanyId = Convert.ToInt32(dr["companyId"]);
                _paymentHistory.Status =dr["status"].ToString();
                _paymentHistory.Type = dr["type"].ToString();
                _paymentHistory.PayMethod = dr["payMethod"].ToString();
                _paymentHistory.StartDt = Convert.ToDateTime(dr["startDt"]);
                _paymentHistory.EndDt = Convert.ToDateTime(dr["endDt"]);
                if (dr["createDt"] != DBNull.Value)
                    _paymentHistory.CreateDt = Convert.ToDateTime(dr["createDt"]);
                if (dr["adminId"] != DBNull.Value)
                    _paymentHistory.AdminId = Convert.ToInt32(dr["adminId"]);
                if (dr["updateStatusDt"] != DBNull.Value)
                    _paymentHistory.UpdateStatusDt = Convert.ToDateTime(dr["updateStatusDt"]);
                if (dr["orderCount"] != DBNull.Value)
                    _paymentHistory.OrderCount = Convert.ToInt32(dr["orderCount"]);
                if (dr["deductMoney"] != DBNull.Value)
                    _paymentHistory.DeductMoney = Convert.ToDecimal(dr["deductMoney"]);
                if (dr["defaultDue"] != DBNull.Value)
                    _paymentHistory.DefaultDue = Convert.ToInt32(dr["defaultDue"]);
            }
            return _paymentHistory;
        }
        public static DataTable listWithExtend(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_PaymentHistory_listWithExtend", companyId);
        }
        /// <summary>
        /// 财务管理汇总
        /// </summary>
        /// <param name="companyId">企业编号</param>
        /// <param name="startDt">开始日期,默认为 NULL</param>
        /// <param name="endDt">结束日期,默认为 NULL</param>
        /// <param name="companyName">企业名称</param>
        /// <param name="companyNo">企业编号</param>
        /// <param name="payMethod">支付类型</param>
        /// <param name="type">账单类型：1表示采购商；2表示供应商</param>
        /// <returns></returns>
        public static DataTable listWithCollect(int companyId, DateTime? startDt, DateTime? endDt, string companyName, string companyNo, string payMethod, string type)
        {
            return DbHelper.ExecuteDataTable("p_PaymentHistory_listWithCollect", companyId,startDt,endDt,companyName,companyNo,payMethod,type);
        }
        public static DataTable listWithPreview(string where)
        {
            return DbHelper.ExecuteDataTable("p_PaymentHistory_listWithPreview", where);
        }

        //public static int insertPayableByFinance(string where,string startDt,string endDt)
        //{
        //    return Convert.ToInt32(DbHelper.ExecuteScalar("p_PaymentHistory_insertPayableByFinance",where,startDt,endDt ));
        //}

        public static int insertPayableByFinance(string where, DateTime? startDt, DateTime? endDt)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_PaymentHistory_insertPayableByFinance", where, startDt, endDt));
        }
    }
}
